package com.june.junemplayer.base;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public abstract class BaseActivity extends AppCompatActivity {
    /**
     * 快速点击的时间间隔
     */
    private static final int FAST_CLICK_DELAY_TIME = 300;
    /**
     * 最后点击的时间
     */
    private static long mLastClickTime;
    /**
     * 上下文参数
     */
    protected Activity mActivity;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("JuneProcedureLog",getTag()+"----onCreate");

        initBeforeView(savedInstanceState);
        this.mActivity = this;

        //添加继承这个BaseActivity的Activity
        BaseApplication.getActivityManager().addActivity(this);
        //绑定布局id
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
        }
        bindView(savedInstanceState);
        //初始化数据
        initData(savedInstanceState);
    }

    protected abstract void initBeforeView(Bundle savedInstanceState);

    protected abstract void bindView(Bundle savedInstanceState);

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract int getLayoutId();

    protected abstract String getTag();

    protected void Back(Toolbar toolbar) {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
                if (!isFastClick()) {
                    mActivity.finish();
                }
            }
        });
    }

    protected void Back(View view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
                if (!isFastClick()) {
                    mActivity.finish();
                }
            }
        });
    }

    protected static boolean isFastClick() {
        boolean flag = true;
        long currentClickTime = System.currentTimeMillis();
        if ((currentClickTime - mLastClickTime) >= FAST_CLICK_DELAY_TIME) {
            flag = false;
        }
        mLastClickTime = currentClickTime;

        return flag;
    }

    protected void show(CharSequence llw) {
        Toast.makeText(mActivity, llw, Toast.LENGTH_SHORT).show();
    }
}